from user_db import login1, register1, logoff1
from user import User
from tkinter import *
from PIL import Image, ImageTk
from tkinter import messagebox
def main():  # enter main
    def goto(num):
        root.destroy()  # destroy main win
        if num == 1:
            login()  # enter win 1
        elif num == 2:
            register()  # enter win 2
        elif num == 3:
            exit()
        elif num == 4:
            logoff()

    root = Tk()
    root.title('Main')
    butmain1 = Button(root, text="登录", command=lambda: goto(1))
    butmain1.pack(pady=5)

    butmain2 = Button(root, text="注册", command=lambda: goto(2))
    butmain2.pack(pady=5)

    butmain3 = Button(root, text="退出", command=lambda: goto(3))
    butmain3.pack(pady=5)

    butmain4 = Button(root, text="注销", command=lambda: goto(4))
    butmain4.pack(pady=5)

    root.mainloop()


def login():
    def gotomain():
        root1.destroy()  # close win 1
        main()  # enter main

    def event1():
        """按钮事件"""
        text_name = w1.get('0.0', 'end').strip()
        text_password = w2.get('0.0', 'end').strip()
        print(f'{text_name}, {text_password}')
        result = login1(text_name, text_password)
        if result:
            print('登录成功')
            messagebox.showinfo('提示', '登录成功')
        else:
            print('登录失败')
            messagebox.showinfo('提示', '登录失败')

    def event2():
        """按钮事件"""
        w1.delete(1.0, "end")
        w2.delete(1.0, "end")


    root1 = Tk()
    root1.geometry('500x200+1100+150')
    root1.title('登录')
    butone1 = Button(root1, text="确认", command=event1)
    butone1.place(relx=0.1, y=160, relwidth=0.2, relheight=0.2)
    butone2 = Button(root1, text="取消", command=event2)
    butone2.place(relx=0.7, y=160, relwidth=0.2, relheight=0.2)
    butone3 = Button(root1, text="返回", command=gotomain)
    butone3.place(relx=0.4, y=160, relwidth=0.2, relheight=0.2)
    Label1 = Label(root1, text="用户姓名：")
    Label1.place(relx=0.2, x=10, y=10, relwidth=0.2, relheight=0.2)

    w1 = Text(root1, width=5, height=5)
    w1.place(relx=0.2, x=85, y=20, relwidth=0.2, relheight=0.1)

    Label2 = Label(root1, text="密码：")
    Label2.place(relx=0.2, x=10, y=40, relwidth=0.2, relheight=0.2)

    w2 = Text(root1, width=5, height=5)
    w2.place(relx=0.2, x=85, y=50, relwidth=0.2, relheight=0.1)
    root1.mainloop()


def register():
    def gotomain():
        root2.destroy()  # close win 2
        main()  # enter main

    def event1():
        """按钮事件"""
        text_name = w1.get('0.0', 'end').strip()
        text_password = w2.get('0.0', 'end').strip()
        text_age = int(w3.get('0.0', 'end').strip())
        text_gender = w4.get('0.0', 'end').strip()
        user = User(text_name, text_password, text_age, text_gender)
        result = register1(user)
        if result:
            print('注册成功')
            messagebox.showinfo('提示', '注册成功')
        else:
            print('注册失败')
            messagebox.showinfo('提示', '注册失败')

    def event2():
        """按钮事件"""
        w1.delete(1.0, "end")
        w2.delete(1.0, "end")
        w3.delete(1.0, "end")
        w4.delete(1.0, "end")

    root2 = Tk()
    root2.geometry('500x200+1100+150')
    root2.title('注册')
    buttwo1 = Button(root2, text="确认", command=event1)
    buttwo1.place(relx=0.1, y=160, relwidth=0.2, relheight=0.2)
    buttwo2 = Button(root2, text="取消", command=event2)
    buttwo2.place(relx=0.7, y=160, relwidth=0.2, relheight=0.2)
    buttwo3 = Button(root2, text="返回", command=gotomain)
    buttwo3.place(relx=0.4, y=160, relwidth=0.2, relheight=0.2)

    Label1 = Label(root2, text="用户姓名：")
    Label1.place(relx=0.2, x=10, y=10, relwidth=0.2, relheight=0.2)

    w1 = Text(root2, width=5, height=5)
    w1.place(relx=0.2, x=85, y=20, relwidth=0.2, relheight=0.1)

    Label2 = Label(root2, text="密码：")
    Label2.place(relx=0.2, x=10, y=40, relwidth=0.2, relheight=0.2)

    w2 = Text(root2, width=5, height=5)
    w2.place(relx=0.2, x=85, y=50, relwidth=0.2, relheight=0.1)

    Label3 = Label(root2, text="年龄：")
    Label3.place(relx=0.2, x=10, y=70, relwidth=0.2, relheight=0.2)

    w3 = Text(root2, width=5, height=5)
    w3.place(relx=0.2, x=85, y=80, relwidth=0.2, relheight=0.1)

    Label4 = Label(root2, text="性别：")
    Label4.place(relx=0.2, x=10, y=100, relwidth=0.2, relheight=0.2)

    w4 = Text(root2, width=5, height=5)
    w4.place(relx=0.2, x=85, y=110, relwidth=0.2, relheight=0.1)

    root2.mainloop()

def exit():
    def gotomain():
        root3.destroy()  # close win 3

    root3 = Tk()
    root3.geometry('400x600+888+444')
    root3.title('win 3')
    photo = Image.open('再见.jpg')
    img = ImageTk.PhotoImage(photo)
    label_img = Label(root3, image=img)
    label_img.pack()
    butthree1 = Button(root3, text="再见", command=gotomain)
    butthree1.pack(pady=10)
    root3.mainloop()

def logoff():
    def gotomain():
        root4.destroy()  # close win 4
        main()

    def event1():
        """按钮事件"""
        user = w1.get('0.0', 'end').strip()
        logoff1(user)
        messagebox.showinfo('提示', '注销成功')

    def event2():
        """按钮事件"""
        w1.delete(1.0, "end")
    root4 = Tk()
    root4.geometry('500x200+1100+150')
    root4.title('注销')
    butfour1 = Button(root4, text="确认", command=event1)
    butfour1.place(relx=0.1, y=160, relwidth=0.2, relheight=0.2)
    butfour2 = Button(root4, text="取消", command=event2)
    butfour2.place(relx=0.7, y=160, relwidth=0.2, relheight=0.2)
    butfour3 = Button(root4, text="返回", command=gotomain)
    butfour3.place(relx=0.4, y=160, relwidth=0.2, relheight=0.2)

    Label1 = Label(root4, text="注销用户：")
    Label1.place(relx=0.2, x=10, y=70, relwidth=0.2, relheight=0.2)

    w1 = Text(root4, width=5, height=5)
    w1.place(relx=0.2, x=85, y=80, relwidth=0.2, relheight=0.1)

    root4.mainloop()
